package com.kexin.student.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.kexin.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.kexin.student.domain.Jkxx;
import com.kexin.student.mapper.StudentMapper;
import com.kexin.student.domain.Student;
import com.kexin.student.service.IStudentService;

/**
 * 学生信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class StudentServiceImpl implements IStudentService 
{
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询学生信息
     * 
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 查询学生信息列表
     * 
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息
     * 
     * @param student 学生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStudent(Student student)
    {
        int rows = studentMapper.insertStudent(student);
        insertJkxx(student);
        return rows;
    }

    /**
     * 修改学生信息
     * 
     * @param student 学生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStudent(Student student)
    {
        studentMapper.deleteJkxxByStuId(student.getId());
        insertJkxx(student);
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除学生信息
     * 
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStudentByIds(Long[] ids)
    {
        studentMapper.deleteJkxxByStuIds(ids);
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     * 
     * @param id 学生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStudentById(Long id)
    {
        studentMapper.deleteJkxxByStuId(id);
        return studentMapper.deleteStudentById(id);
    }

    /**
     * 新增交款信息信息
     * 
     * @param student 学生信息对象
     */
    public void insertJkxx(Student student)
    {
        List<Jkxx> jkxxList = student.getJkxxList();
        Long id = student.getId();
        if (StringUtils.isNotNull(jkxxList))
        {
            List<Jkxx> list = new ArrayList<Jkxx>();
            for (Jkxx jkxx : jkxxList)
            {
                jkxx.setStuId(id);
                list.add(jkxx);
            }
            if (list.size() > 0)
            {
                studentMapper.batchJkxx(list);
            }
        }
    }
}
